package com.zengge.zbc.service.impl;

import com.zengge.zbc.dto.Room;
import com.zengge.zbc.exception.ErrorCode;
import com.zengge.zbc.exception.ServiceException;
import com.zengge.zbc.mapper.DeviceInfoMapper;
import com.zengge.zbc.mapper.RoomInfoMapper;
import com.zengge.zbc.po.DeviceInfo;
import com.zengge.zbc.po.RoomInfo;
import com.zengge.zbc.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {


    private final RoomInfoMapper roomInfoMapper;
    private final DeviceInfoMapper deviceInfoMapper;
    @Autowired
    public RoomServiceImpl(RoomInfoMapper roomInfoMapper,
                           DeviceInfoMapper deviceInfoMapper) {
        this.roomInfoMapper = roomInfoMapper;
        this.deviceInfoMapper = deviceInfoMapper;
    }

    private void checkPermission(RoomInfo roomInfo, long userId) {
        if (roomInfo.getUserObjId() != userId)
            throw ServiceException.toError(ErrorCode.NOT_PERMISSION);
    }


    public int maxAddress(List<RoomInfo> roomInfos) {
        int maxAddress = 1;
        for (RoomInfo roomInfo : roomInfos) {
            if (maxAddress < roomInfo.getAddress()) {
                maxAddress = roomInfo.getAddress();
            }
        }
        return maxAddress;
    }

    @Transactional
    @Override
    public String saveOrUpdateRoomInfo(Room room, long userId) {
        String roomId = room.getRoomId();
        if (roomId != null) {
            RoomInfo roomInfo = roomInfoMapper.findRoomInfoByObjectId(Long.parseLong(roomId));
            if (roomInfo == null)
                throw ServiceException.toError(ErrorCode.ROOM_NOT_FOUND);
            roomInfo.setRoomName(room.getRoomName());
            roomInfoMapper.update(roomInfo);
            return String.valueOf(roomInfo.getObjectId());
        } else {
            List<RoomInfo> roomInfos = roomInfoMapper.findRoomInfosByUserObjId(userId);
            int maxAddress = maxAddress(roomInfos);
            RoomInfo roomInfo = new RoomInfo(room.getRoomName(), userId,(maxAddress+1));
            roomInfoMapper.save(roomInfo);
            return String.valueOf(roomInfo.getObjectId());
        }
    }

    @Override
    public List<Room> roomListGet(long userId) {
        List<RoomInfo> roomInfos = roomInfoMapper.findRoomInfosByUserObjId(userId);
        List<Room> rooms = new ArrayList<>(roomInfos.size());
        for (RoomInfo roomInfo : roomInfos) {
            rooms.add(new Room(String.valueOf(roomInfo.getObjectId()),
                    roomInfo.getRoomName(),roomInfo.getAddress()));
        }
        return rooms;
    }

    @Override
    public Room roomGet(String roomId, long userId) {
        RoomInfo roomInfo = roomInfoMapper.findRoomInfoByObjectId(Long.parseLong(roomId));
        if (roomInfo == null)
            throw ServiceException.toError(ErrorCode.ROOM_NOT_FOUND);
        return new Room(String.valueOf(roomInfo.getObjectId()), roomInfo.getRoomName(),roomInfo.getAddress());
    }

    @Transactional
    @Override
    public void deleteRoomId(String roomId) {
        RoomInfo roomInfo =roomInfoMapper.findRoomInfoByObjectId(Long.parseLong(roomId));
        if (roomInfo == null)
            return;
        List<DeviceInfo> deviceInfos = deviceInfoMapper.findDeviceInfoListByRoomObjId(roomInfo.getObjectId());
        for (DeviceInfo deviceInfo:deviceInfos){
            deviceInfoMapper.delete(deviceInfo);
        }
        roomInfoMapper.delete(roomInfo);
    }
}
